﻿using System;
using System.Collections.Generic;

using BizElements.Core.ProviderComponents;

namespace BizElements.Core
{
    /// <summary>Default strategy/algorithm for entity DAO creation used in BizElements library.</summary>
    /// <remarks>Default algorithm is simple. If IEntity object also implements IEntityDAO interface then it is returned
    /// by <see cref="GetEntityDAO"/> method.
    /// If no DAO has been created in previous steps, then the algorithm defaults to generic <see cref="EntityDAO"/> 
    /// object which will work the given entity.</remarks>
    public sealed class DefaultEntityDaoCreationStrategy : IEntityDaoCreationStrategy
    {
        /// <summary>Gets DAO for the given entity.</summary>
        /// <param name="entity">Entity which the returned DAO will work with.</param>
        /// <param name="conn">Connection context the DAO will use. If null, default DAO settings are used.</param>
        /// <returns>IEntityDAO implementation.</returns>
        public IEntityDAO GetEntityDAO(IEntity entity, IConnectionProvider conn)
        {
            IEntityDAO dao;
            IEntityDAO selfServicedEntity = entity as IEntityDAO;
            if (selfServicedEntity != null)
                dao = selfServicedEntity;
            else
                dao = new EntityDAO(entity);

            if (conn != null)
                dao.ConnectionProvider = conn;

            return dao;
        }
    }
}
